//
// Created by root on 19-10-18.
//

#ifndef OPENGLESPRO_GAME_STEP_3_H
#define OPENGLESPRO_GAME_STEP_3_H

#define VERTEX_ATTR_INDEX_V3_POSITION 0
#define VERTEX_ATTR_INDEX_V3_COLOR 1
#define VERTEX_ATTR_INDEX_V3_MATRIX 2
#define VERTEX_VEC_COUNT_V3_PER_POSITION 2
#define VERTEX_VEC_COUNT_V3_PER_COLOR 3
#define VERTEX_VEC_COUNT_V3_PER_TOTAL (VERTEX_VEC_COUNT_V3_PER_POSITION+VERTEX_VEC_COUNT_V3_PER_COLOR)
#define STRIDE_BYTES_V3 VERTEX_VEC_COUNT_V3_PER_TOTAL*sizeof(GLfloat)

void OnSurfaceCreatedV3();

void OnSurfaceCreatedV3() {}

void OnSurfaceChangedV3();

void OnSurfaceChangedV3() {}

void OnDrawFrameV3();

void OnDrawFrameV3() {
    GLfloat vertexPointers[] = {
            // Order of coordinates: X, Y, R, G, B

            // Triangle Fan
            0, 0, 1, 1, 1,
            -0.5, -0.8, 0.7, 0.7, 0.7,
            0.5, -0.8, 0.7, 0.7, 0.7,
            0.5, 0.8, 0.7, 0.7, 0.7,
            -0.5, 0.8, 0.7, 0.7, 0.7,
            -0.5, -0.8, 0.7, 0.7, 0.7,

            // Line
            -0.5, 0, 1, 0, 0,
            0.5, 0, 1, 0, 0,

            // Mallets
            0, -0.4, 0, 0, 1,
            0, 0.4, 1, 0, 0
    };
    // 原始坐标矩阵
    GLfloat originalMatrix[] = {
            1, 0, 0, 0,
            0, 1, 0, 0,
            0, 0, 1, 0,
            0, 0, 0, 1
    };
    int w = device.width;
    int h = device.height;
    float aspectRatio = w > h ?
                        (float) w / (float) h : // 横屏
                        (float) h / (float) w;  // 竖屏
    // 横屏坐标矩阵
    GLfloat landMatrix[] = {
            1 / aspectRatio, 0, 0, 0,
            0, 1, 0, 0,
            0, 0, 1, 0,
            0, 0, 0, 1
    };
    // 竖屏坐标矩阵
    GLfloat portraitMatrix[] = {
            1, 0, 0, 0,
            0, 1 / aspectRatio, 0, 0,
            0, 0, 1, 0,
            0, 0, 0, 1
    };
    // 利用矩阵进行正交投影
    glUniformMatrix4fv(VERTEX_ATTR_INDEX_V3_MATRIX, 1, GL_FALSE,
                       w > h ? landMatrix : portraitMatrix);
    glLineWidth(3);
    glEnableVertexAttribArray(VERTEX_ATTR_INDEX_V3_COLOR);
    glEnableVertexAttribArray(VERTEX_ATTR_INDEX_V3_POSITION);
    glVertexAttribPointer(VERTEX_ATTR_INDEX_V3_POSITION, VERTEX_VEC_COUNT_V3_PER_POSITION, GL_FLOAT,
                          GL_FALSE, STRIDE_BYTES_V3, vertexPointers);
    glVertexAttribPointer(VERTEX_ATTR_INDEX_V3_COLOR, VERTEX_VEC_COUNT_V3_PER_COLOR, GL_FLOAT,
                          GL_FALSE, STRIDE_BYTES_V3, &vertexPointers[2]);
    glDrawArrays(GL_TRIANGLE_FAN, 0, 6);
    glDrawArrays(GL_LINES, 6, 2);
    glDrawArrays(GL_POINTS, 8, 1);
    glDrawArrays(GL_POINTS, 9, 1);
}

#endif //OPENGLESPRO_GAME_STEP_3_H
